package com.dengqi.mymodule.wxapi;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

import com.module.umlib.WXHelper;
import com.module.umlib.utils.ConstantsUtils;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import androidx.annotation.Nullable;

/**
 * 微信支付回调
 * Created by cheng on 2017/11/16.
 */
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private IWXAPI api;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, ConstantsUtils.WX_APP_ID, false);
        api.handleIntent(getIntent(), this);
    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }


    @Override
    public void onResp(BaseResp baseResp) {
        //微信原生支付
        if (baseResp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            switch (baseResp.errCode) {
                case 0:
                    WXHelper.onWxPayListener.onComplete();
                    Toast.makeText(getApplicationContext(), "支付成功", Toast.LENGTH_SHORT);
                    finish();
                    break;
                case -1:
                    WXHelper.onWxPayListener.onCancel();
                    Toast.makeText(getApplicationContext(), "支付取消", Toast.LENGTH_SHORT);
                    finish();
                    break;
                case -2:
                    WXHelper.onWxPayListener.onFailed();
                    Toast.makeText(getApplicationContext(), "请求失败", Toast.LENGTH_SHORT);
                    finish();
                    break;
            }
        }
    }

}
